package com.doris.lucene.servlet;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.doris.lucene.Constants;
import com.doris.lucene.LuceneIndexer;
import com.doris.lucene.LuceneProperties;

/**
 * 本地目录爬取，生成索引
 * 
 * @author root
 *
 */
public class SettingServlet extends HttpServlet {

	private static final long serialVersionUID = 7332784929742119507L;
	private static final Logger LOGGER = Logger.getLogger(SettingServlet.class);
	/**
	 * 是否正在写索引
	 */
	private static final String INDEXING = "indexing";
	/**
	 * 爬取目录
	 */
	private static final String SEARCH_DIRS = "searchDirs";

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding(Constants.UTF8);
		if (LuceneProperties.getLockedDir().equals(Constants.DEFAULT_LOCKED_DIR)) { // 没有正在运行的索引
			request.setAttribute(INDEXING, "no");
		} else {
			request.setAttribute(INDEXING, "yes");
		}
		request.setAttribute(SEARCH_DIRS, getSearchDirs()); // 爬取目录
		request.setAttribute("lastIndexDate", getLastIndexDate()); // 最后一次爬取时间
		request.getRequestDispatcher("setting.jsp").forward(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		if (!LuceneProperties.getLockedDir().equals(Constants.DEFAULT_LOCKED_DIR)) { // 索引正在运行
			doGet(request, response);
			return;
		}
		request.setCharacterEncoding(Constants.UTF8);
		String searchDirs = (String) request.getParameter(SEARCH_DIRS); // 爬取目录
		if (searchDirs != null && !searchDirs.isEmpty()) {
			List<Object> dirs = new ArrayList<Object>();
			for (Object dir : searchDirs.split(System.lineSeparator())) {
				if (dir != null && !((String) dir).isEmpty()) {
					dirs.add(dir);
				}
			}
			LuceneProperties.setSearchDirs(dirs);
			new LuceneIndexer().index();
		}
		doGet(request, response);
	}

	/**
	 * 获取要爬取的目录
	 * 
	 * @return 爬取目录
	 */
	private String getSearchDirs() {
		String search = "";
		List<Object> searchDirs = LuceneProperties.getSearchDirs();
		for (Object searchDir : searchDirs) {
			search += ((String) searchDir).replaceAll("\\\\", "&#92;") + "<br/>";
		}
		return search;
	}

	/**
	 * 获取上一次写索引的时间
	 * 
	 * @return 上一次写索引的时间
	 */
	private String getLastIndexDate() {
		Date tmpDate = null;
		String rootDir = LuceneProperties.getIndexDir();
		if (!new File(rootDir).exists()) {
			new File(rootDir).mkdirs();
		}
		for (File file : new File(rootDir).listFiles()) {
			if (file.isFile()) {
				continue;
			}

			try {
				Date date = new SimpleDateFormat(Constants.DATE_TIME_FORMAT).parse(file.getName());
				if (date != null && (tmpDate == null || date.compareTo(tmpDate) > 0)) {
					tmpDate = date;
				}
			} catch (java.text.ParseException e) {
				LOGGER.error("索引更新时间格式化失败", e);
			}
		}
		if (tmpDate != null) {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tmpDate);
		}
		return null;
	}

}
